var maplet = null;
$(document).ready(function () {
    initMap();
    initMessage();
    bindEvent();
    initOverlays();
});

function bindEvent() {

}

function initMap() {
    maplet = new Maplet("mmap");
    maplet.centerAndZoom(new MPoint(0, 0), 0);

    document.getElementById('LayerScale').style.display = 'none';
    document.getElementById('ImgCopyright').style.display = 'none';
    $('span:contains("GS(2011)1617")').parent().hide();
    onWindowResize();
}

function initMessage() {

}

function initOverlays() {
    renderMMarker(new MPoint(0, 0),'','','AP03');
    renderMMarker(new MPoint(15, 3), 'resources/images/icon06.png', '', '习大大');
    renderMMarker(new MPoint(-15, 33), 'resources/images/icon09.png', 'single_red', '小马');

    var polyline = renderMPolyline([
        new MPoint(-42.59826, 37.68081), new MPoint(-3.00075, 38.15571), new MPoint(-3.00047, 26.39692), new MPoint(-42.59783, 26.39828)
    ]);
    var isRed = false;
    setInterval(function(){
        var brush = new MBrush("green");
        brush.arrow = false;
        brush.style = 0;
        brush.stroke = 1;
        brush.fill = true;
        brush.bgcolor =  isRed ? "red" : "blue";
        brush.color = "red";
        brush.bgtransparency = 35;

        polyline.setBrush(brush);

        isRed = !isRed;
    }, 500);
}

function renderMMarker(point, icon, clazz, name) {
    icon = icon || 'resources/images/wifi-flat.png';
    clazz = clazz || 'single';
    name = name || '';
    marker = new MMarker(
        point,
        new MIcon(icon, 40, 40),
        null,
        new MLabel("<div class='" + clazz + "'><em>" + name + "</em></div>", {
            xoffset: 0,
            yoffset: 0,
            opacity: 100,
            enableStyle: false
        })
    );

    maplet.addOverlay(marker);
    return marker;
}

function renderMPolyline(points) {
    var brush = new MBrush("green");
    brush.arrow = false;
    brush.style = 0;
    brush.stroke = 1;
    brush.fill = true;
    brush.bgcolor = "red";
    brush.color = "red";
    brush.bgtransparency = 35;
    var polyline = new MPolyline(
        points,
        brush
    );
    maplet.addOverlay(polyline);
    return polyline;
}

function onWindowResize() {
    $('#clear').css("left", $('#mmap').position().left);
    $('#clear').css("top", $('#mmap').position().top);
}